package cn.mw.cmdb.entity.modelRelation;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Node(labels = "ModelAsset")
public class ModelAsset {

    @Id
    private String id;

    @Relationship(type = "ModelRelate", direction = Relationship.Direction.OUTGOING)
    private List<ModelRelate> outModelRelates;

    @Relationship(type = "ModelRelate", direction = Relationship.Direction.INCOMING)
    private List<ModelRelate> inModelRelates;

    private ModelRelationInfo modelRelationInfo;

    private int level;

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public ModelRelationInfo getModelRelationInfo() {
        return modelRelationInfo;
    }

    public void setModelRelationInfo(ModelRelationInfo modelRelationInfo) {
        this.modelRelationInfo = modelRelationInfo;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void addOutModelRelate(ModelRelate modelRelate) {
        if (null == outModelRelates) {
            outModelRelates = new ArrayList<>();
        }
        outModelRelates.add(modelRelate);
    }

    public List<ModelRelate> getOutModelRelates() {
        return outModelRelates;
    }

    public void addInModelRelate(ModelRelate modelRelate) {
        if (null == inModelRelates) {
            inModelRelates = new ArrayList<>();
        }
        inModelRelates.add(modelRelate);
    }

    public List<ModelRelate> getInModelRelates() {
        return inModelRelates;
    }

    public List<ModelRelate> getAllRelates() {
        List<ModelRelate> allModelRelates = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(outModelRelates)) {
            allModelRelates.addAll(outModelRelates);
        }
        if (CollectionUtils.isNotEmpty(inModelRelates)) {
            allModelRelates.addAll(inModelRelates);
        }
        return allModelRelates;
    }

    public void extractFrom(ModelAsset modelAsset) {
        this.id = modelAsset.getId();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ModelAsset that = (ModelAsset) o;
        return Objects.equals(id, that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}
